package com.company.bookstore.mapper.book_module;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.company.bookstore.entities.book_module.BookCategory;
import com.company.bookstore.entities.book_module.Category;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 姬深威
 * @since 2020-09-12
 */
public interface CategoryMapper extends BaseMapper<Category> {

    @Select("SELECT t_category.* FROM t_category,t_book_category " +
            "WHERE t_book_category.fk_book_id=#{bookId} " +
            "AND t_book_category.fk_category_id=t_category.id")
    List<Category> getCategoryByBookId(Integer bookId);

    @Select("SELECT * FROM t_category WHERE id=#{id}")
    Category getCategoryById(Integer id);

    @Select("SELECT * FROM t_category")
    List<Category> getAllCategories();

    @Insert("INSERT INTO t_category (name) VALUES(#{name})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyColumn = "id", keyProperty = "id", resultType = Integer.class, before = false)
        //返回上一条插入的id
    Integer addCategory(Category category);


    @Update("UPDATE t_category SET name=#{name} WHERE id=#{id};")
    Integer updateCategory(Category category);

    @Delete("DELETE FROM t_category WHERE id=#{cId};")
    Integer deleteCategory(Integer cId);
}
